package com.liuf.quartz.controller;

import com.liuf.quartz.common.CommonResponse;
import com.liuf.quartz.common.ResponseUtil;
import com.liuf.quartz.domain.ScheduleJob;
import com.liuf.quartz.exception.ServiceException;
import com.liuf.quartz.service.IJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: spring-cloud-liuf-master
 * @description: Common constant
 * @author: Mr.Liu
 * @create: 2018-08-30 13:25
 * @码云： https://gitee.com/www.jiandan.com
 **/
@Api(value = "调度工具接口API")
@RestController
@RequestMapping(value = "/api/quartz")
public class QuartzController {

    private final Logger logger = LoggerFactory.getLogger(QuartzController.class);

    @Autowired
    private IJobService jobService;

    @ApiOperation(value = "获取所有调度任务")
    @GetMapping
    public CommonResponse getAllJob() {
        return ResponseUtil.generateResponse(jobService.getAllJob());
    }

    @ApiOperation(value = "根据ID(主键)获取任务")
    @GetMapping("/{id}")
    public CommonResponse getJob(@ApiParam(value = "主键ID") @PathVariable("id") Long jobId) throws ServiceException {
        return ResponseUtil.generateResponse(jobService.select(jobId));
    }

    @ApiOperation(value = "根据ID(主键)修改任务")
    @PutMapping("/update/{id}")
    public CommonResponse updateJob(@ApiParam(value = "主键ID") @PathVariable("id") Long jobId, @ApiParam(value = "调度基础信息") @RequestBody ScheduleJob newScheduleJob) throws ServiceException {
        return ResponseUtil.generateResponse(jobService.update(jobId, newScheduleJob));
    }
    @ApiOperation(value = "根据ID主键删除任务")
    @DeleteMapping("/delete/{id}")
    public CommonResponse deleteJob(@PathVariable("id") Long jobId) throws ServiceException {
        return ResponseUtil.generateResponse(jobService.delete(jobId));
    }

    @ApiOperation(value = "存储任务")
    @PostMapping("/save")
    public CommonResponse saveJob(@RequestBody ScheduleJob newScheduleJob) throws ServiceException {
        return ResponseUtil.generateResponse(jobService.add(newScheduleJob));
    }

    @ApiOperation(value = "按照运行任务ID")
    @PutMapping("/run/{id}")
    public CommonResponse runJob(@ApiParam(value = "任务主键ID")@PathVariable("id") Long jobId) throws ServiceException {
        return ResponseUtil.generateResponse(jobService.run(jobId));
    }

    @ApiOperation(value = "暂停任务")
    @PutMapping("/pause/{id}")
    public CommonResponse pauseJob(@ApiParam(value = "主键ID") @PathVariable("id") Long jobId) throws ServiceException {
        return ResponseUtil.generateResponse(jobService.pause(jobId));
    }

    @ApiOperation(value = "恢复任务")
    @PutMapping("/resume/{id}")
    public CommonResponse resumeJob(@ApiParam(value = "主键ID")@PathVariable("id") Long jobId) throws ServiceException {
        return ResponseUtil.generateResponse(jobService.resume(jobId));
    }
}
